package com.mission.schedule.service;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.mission.schedule.R;
import com.mission.schedule.activity.AlarmDialog;
import com.mission.schedule.activity.MainActivity;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.utils.SharedPrefUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class ClockService extends Service {// implements OnCompletionListener
    String alarmType;
    String ringcode;
    String alarmSound;
    int cdId;
    String morningstate;
    String nightstate;
    String alarmclocktime;
    String before;
    String content = "";
    String alltimestate;
    String ringstate;
    String displaytime;
    String postpone;
    String alarmSoundDesc;
    String isalarmtype;
    String stateone = "";
    String statetwo = "";
    String dateone = "";
    String datetwo = "";
    private String WriteAlarmClockwrite = "0";
    private String notificationStr = "";
    NotificationManager notificationManager = null;

    MediaPlayer mediaPlayer = null;

    SharedPrefUtil sharedPrefUtil = null;


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @SuppressLint("NewApi")
    @Override
    public void onCreate() {
        super.onCreate();
        sharedPrefUtil = new SharedPrefUtil(getApplication(), ShareFile.USERFILE);
        mediaPlayer = new MediaPlayer();
        notificationStr = sharedPrefUtil.getString(getApplicationContext(), ShareFile.USERFILE, ShareFile.ISNOTIFICATION, "0");
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.mipmap.logo).setContentTitle("时间表").setContentText("时间表正在运行中!");
        Intent intents = new Intent(getApplicationContext(), MTimeService.class);
        startService(intents);
        Notification notification = builder.build();
        notification.flags |= Notification.FLAG_ONGOING_EVENT;// 通知放前台
        notification.flags |= Notification.FLAG_NO_CLEAR;// 通知不能清理
        if ("0".equals(notificationStr)) {
            startForeground(0x1982, notification); // notification ID: 0x1982, you
        } else {
            startForeground(0, notification); // notification ID: 0x1982, you
        }
    }

    @SuppressLint("NewApi")
    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        if (intent == null) {
            return;
        }
        Log.e("TAG","ClockService3");

        synchronized (this) {
            toData(intent);
        }

    }

    private void toData(Intent intent) {
        WriteAlarmClockwrite = intent.getStringExtra("WriteAlarmClockwrite");
        if (WriteAlarmClockwrite != null && WriteAlarmClockwrite.equals("1")) {
            alarmType = intent.getStringExtra("alarmType");
            isalarmtype = intent.getStringExtra("isalarmtype");
            ringcode = intent.getStringExtra("ringcode");
            alarmSound = intent.getStringExtra("alarmSound");
            alarmSoundDesc = intent.getStringExtra("alarmSoundDesc");
            cdId = intent.getIntExtra("cdId", 0);
            morningstate = intent.getStringExtra("morningstate");
            nightstate = intent.getStringExtra("nightstate");
            alarmclocktime = intent.getStringExtra("alarmclocktime");
            before = intent.getStringExtra("before");
            content = intent.getStringExtra("content");
            alltimestate = intent.getStringExtra("alltimestate");
            displaytime = intent.getStringExtra("displaytime");
            postpone = intent.getStringExtra("postpone");
            stateone = intent.getStringExtra("stateone");
            statetwo = intent.getStringExtra("statetwo");
            dateone = intent.getStringExtra("dateone");
            datetwo = intent.getStringExtra("datetwo");
            ringstate = sharedPrefUtil.getString(this, ShareFile.USERFILE, ShareFile.RINGSTATE, "0");

            if ("默认".equals(alarmSoundDesc)) {
                alarmSoundDesc = "完成任务";
                ringcode = "g_88";
            }
            if ("".equals(ringcode)) {
                ringcode = "g_88";
            }
            if (cdId < 0) {
                if (cdId == -1) {
                    content = "提醒整理您的待办事项";
                } else if (cdId == -10) {
                    content = "请打开您的时间表，每五分钟测试一次！";
                } else {
                    content = "查看是否还有未完成的事情";
                }
            } else if (cdId > 0 && "0".equals(displaytime)) {
                content = "今天有待办的事情需要完成";
            }

            if (notificationManager == null) {
                notificationManager = (NotificationManager) getApplication().getSystemService(Context.NOTIFICATION_SERVICE);
            }
            NotificationManager nm = (NotificationManager) getApplication().getSystemService(Context.NOTIFICATION_SERVICE);
            Intent notificationIntent = new Intent(getApplication(), MainActivity.class);
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            PendingIntent contentIntent = PendingIntent.getActivity(getApplication(), 0, notificationIntent, 0);

            NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
            builder.setSmallIcon(R.mipmap.logo).setContentTitle("时间表").setContentText(content).setContentIntent(contentIntent);
            Notification notification = builder.build();
            notification.defaults |= Notification.DEFAULT_VIBRATE;
            notification.defaults |= Notification.DEFAULT_LIGHTS;
            notification.ledOnMS = 300;
            notification.ledOffMS = 500;
            notification.flags = Notification.FLAG_SHOW_LIGHTS;
            notification.flags |= Notification.FLAG_AUTO_CANCEL;// 在通知栏上点击此通知后自动清除此通知

            nm.notify(cdId, notification);

            // 桌面显示提示
            Intent i = new Intent(getApplication(), AlarmDialog.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.putExtra("alarmType", alarmType);
            i.putExtra("isalarmtype", isalarmtype);
            i.putExtra("ringcode", ringcode);
            i.putExtra("alarmSound", alarmSound);
            i.putExtra("alarmSoundDesc", alarmSoundDesc);
            i.putExtra("cdId", cdId);
            i.putExtra("morningstate", morningstate);
            i.putExtra("nightstate", nightstate);
            i.putExtra("alarmclocktime", alarmclocktime);
            i.putExtra("before", before);
            i.putExtra("content", content);
            i.putExtra("alltimestate", alltimestate);
            i.putExtra("displaytime", displaytime);
            i.putExtra("postpone", postpone);
            i.putExtra("stateone", stateone);
            i.putExtra("statetwo", statetwo);
            i.putExtra("dateone", dateone);
            i.putExtra("datetwo", datetwo);
            if (alarmType.equals("5")) {
                try {
                    SimpleDateFormat simpleDateFormats = new SimpleDateFormat("yyyy-MM-dd");
                    Date date1 = new Date(System.currentTimeMillis());
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(simpleDateFormats.parse(simpleDateFormats.format(date1)));
                    if (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && calendar.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
                        getApplication().startActivity(i);
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            } else {
                getApplication().startActivity(i);
            }
        }
    }


    @Override
    public void onDestroy() {

        super.onDestroy();
    }
}
